<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Manager_User extends Controller_Manager_Template_Default{
       
    private $view;
    private $model;
    private $result;   
    
    function before()
    {       
        parent::before();
        
        if (!Auth::instance()->logged_in())             
        {                            			
            $this->request->redirect('manager/login');
        }
        
        $this->model = ORM::factory('user');
    }
    
    function action_index()
    {        
        
        $this->model = ORM::factory('user');
        $this->result = $this->model->find_all();

        $list = array();
        foreach($this->result as $user) {
             $obj = new stdClass();
             $obj->fullname = $user->fullname;
             $obj->email = $user->email;
             $obj->id = $user->id;
             
             $list[] = $obj;
        }
                
        $this->view = View::factory('template/user/list');
        $this->view->set('list', $list);        
        $this->template->content = $this->view;
        
    }
    
    function action_create()
    {
        
        $id = $this->request->param('id');
        if(!empty($id))
        {            
            $this->model = ORM::factory('user', $id);
        }
         
        $this->view = View::factory('template/user/create');
        $this->view->set('model', $this->model);        
        $this->view->set('user_id', $id);        
        $this->template->content = $this->view;
        
    }
    
    function action_delete()
    {
        $this->model = ORM::factory('user',$this->request->post('id'));
        $this->model->delete();
        
        exit;
    }
    
    function action_deleteselected()
    {
        
        $ids = $this->request->post('id');                
        $this->result = DB::delete('users')->where('id', 'IN', $ids)->execute();

        if($this->result > 0)
        {
            echo "1";
        }
        else
        {
            echo "0";
        }
            
        exit;
    }    
    
    function action_save() 
    {          
        $this->model = ORM::factory('user', $this->request->post('id'));
        $this->model->fullname = $this->request->post('fullname');               
        $this->model->email    = $this->request->post('email');        
        
        $passwd = $this->request->post('password');
        
        if($this->model->loaded())
        {
            try
            {
                if(!empty($passwd))
                    $this->model->password = $this->request->post('password');
                
                $this->model->update();            
                
                echo json_encode(array("success"=>"1"));
            }  
            catch (ORM_Validation_Exception $e)
            {
                echo json_encode($e->errors('models'));
            }                        
        }
        else
        {
            $this->model->password = $this->request->post('password');
            try
            {                               
                $this->model->save();
                
                if (!$this->model->has('roles', 1))
                {
                    $this->model->add('roles', 1);
                }

                if (!$this->model->has('roles', 2))
                {
                    $this->model->add('roles', 2);
                }    
                
                $this->model->save();
                
                echo json_encode(array("success"=>"1"));
                
            }  
            catch (ORM_Validation_Exception $e)
            {
                echo json_encode($e->errors('models'));
            }
            
        }        
        
        exit;
    }
}
?>
